package erp.server.organization.service.impl;

import erp.server.base.enums.RoleLevelCode;
import erp.server.base.enums.RoleStatusCode;
import erp.server.base.exception.ServiceException;
import erp.server.organization.mapper.OrganizationMapper;
import erp.server.organization.pojo.dto.OrganizationAddParam;
import erp.server.organization.pojo.dto.OrganizationQueryParam;
import erp.server.organization.pojo.entity.Organization;
import erp.server.organization.pojo.vo.OrganizationVO;
import erp.server.organization.service.OrganizationService;
import erp.server.role.pojo.dto.RoleAddParam;
import erp.server.role.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

import static erp.server.base.response.StatusCode.ORGANIZATION_NAME_EXIST;

@Slf4j
@Service
public class OrganizationServiceImpl implements OrganizationService {

    @Autowired
    private OrganizationMapper organizationMapper;
    @Autowired
    private RoleService roleService;

    //创建组织
    @Override
    public void addOrganization(OrganizationAddParam organizationAddParam) {
        log.debug("organizationAddParam-service: {}", organizationAddParam);
        Organization organization = new Organization();
        BeanUtils.copyProperties(organizationAddParam,organization);
        if (organizationAddParam.getId() == null){
            //判断同一个用户是否重复创建相同的组织
            OrganizationQueryParam organizationQueryParam = new OrganizationQueryParam();
            organizationQueryParam.setName(organizationAddParam.getName());
            //通过组织名查询组织的存在
            List<OrganizationVO> organizationVOS = organizationMapper.select(organizationQueryParam);
            for (OrganizationVO organizationVO : organizationVOS) {
                //如果查询出来的用户和创建组织的用户相同，则抛出异常
                if (organizationVO.getUserId().equals(organizationAddParam.getUserId())){
                    throw new ServiceException(ORGANIZATION_NAME_EXIST);
                }
            }

            //创建组织
            organization.setCreatedAt(new Date());
            organizationMapper.insert(organization);

            //在创建组织的同时，加入到角色表，指明是谁创建的
            RoleAddParam roleAddParam = new RoleAddParam();
            roleAddParam.setUserId(organizationAddParam.getUserId());
            roleAddParam.setOrgId(organization.getId());
            roleAddParam.setRole(organizationAddParam.getRole());
            roleAddParam.setStatus(RoleStatusCode.ADDED.getCode());
            roleAddParam.setLevel(RoleLevelCode.LEVEL_SUPER_ADMIN.getCode());
            roleService.insert(roleAddParam);
        }else {
            organization.setUpdatedAt(new Date());
            organizationMapper.update(organization);
        }
    }

    //查询组织
    @Override
    public List<OrganizationVO> select(OrganizationQueryParam organizationQueryParam) {
        log.debug("organizationQueryParam-service:{}",organizationQueryParam);
        List<OrganizationVO> organizationVOList = organizationMapper.select(organizationQueryParam);
        log.debug("organizationVOList-service:{}", organizationVOList);
        return organizationVOList;
    }

    //删除组织
    @Override
    public void delete(Integer id) {
        log.debug("id-service:{}", id);
        organizationMapper.deleteByOrganizationId(id);
    }
}
